﻿using System;
using System.IO;
using System.Web;
using System.Text;
using System.Xml.Serialization;
using System.Xml;

namespace XMLConfig
{
    #region 配置对象模型类

    /// 配置的属性类（注意相关属性的类型与[XmlElement]）
    /// use:ModuleSettings settings=ModuleConfig.GetSettings();
    /// </summary>
    public class XMLSettings
    {
        private string terminalID;
        private string merchantID;
        private string merchantName;
        private string password;
        private int udp_CITIC = 5162;
        private string connectionString = "TEST";
        private string webServiceUrlB2c = "http://hbpiao.3322.org:6000/AirLineTicket/WebServices/WebServices.asmx";
        private string webServiceUrlCitic = "https://pay.bank.ecitic.com/Payment/services/PaymentServices";
        private string citicCertFile = "cfca_citic.pfx";
        private string citicCertPassword = "wh95161";
        private int settlementTime = 5;

        /// <summary>
        /// 自动结算的时间
        /// </summary>
        [XmlElement]
        public int SettlementTime
        {
            set { settlementTime = value; }
            get { return settlementTime; }
        }

        /// <summary>
        /// 中信银行证书文件名
        /// </summary>
        [XmlElement]
        public string CiticCertFile
        {
            set { citicCertFile = value; }
            get { return citicCertFile; }
        }

        /// <summary>
        /// 中信银行证书密码
        /// </summary>
        [XmlElement]
        public string CiticCertPassword
        {
            set { citicCertPassword = value; }
            get { return citicCertPassword; }
        }

        /// <summary>
        /// 终端号
        /// </summary>
        [XmlElement]
        public string TerminalID
        {
            set { terminalID = value; }
            get { return terminalID; }
        }

        /// <summary>
        /// 商户号
        /// </summary>
        [XmlElement]
        public string MerchantID
        {
            set { merchantID = value; }
            get { return merchantID; }
        }

        /// <summary>
        /// 商户名称
        /// </summary>
        [XmlElement]
        public string MerchantName
        {
            set { merchantName = value; }
            get { return merchantName; }
        }

        /// <summary>
        /// 密码
        /// </summary>
        [XmlElement]
        public string Password
        {
            set { password = value; }
            get { return password; }
        }

        /// <summary>
        /// 中信银行支付监听 UDP 端口
        /// </summary>
        [XmlElement]
        public int UDP_CITIC
        {
            set { udp_CITIC = value; }
            get { return udp_CITIC; }
        }

        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        [XmlElement]
        public string ConnectionString
        {
            set { connectionString = value; }
            get { return connectionString; }
        }

        /// <summary>
        /// b2c 系统的 WebService 地址
        /// </summary>
        [XmlElement]
        public string WebServiceUrlB2c
        {
            set { webServiceUrlB2c = value; }
            get { return webServiceUrlB2c; }
        }

        /// <summary>
        /// 中信银行的 WebService 支付网关地址
        /// </summary>
        [XmlElement]
        public string WebServiceUrlCitic
        {
            set { webServiceUrlCitic = value; }
            get { return webServiceUrlCitic; }
        }
    }
    #endregion

    #region  配置的操作类
    /// <summary>
    /// 配置的操作类
    /// </summary>
    public class Operation
    {
        static object lockObject = new object();

        public static XMLSettings GetSettings()
        {
            XMLSettings data = null;
            XmlSerializer serializer;

            try
            {
                serializer = new XmlSerializer(typeof(XMLSettings));
            }
            catch(Exception e)
            {
                throw new Exception("序列化失败,错误信息是:" + e.Message);
            }

            lock (lockObject)
            {
                try
                {
                    string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLConfig.xml");
                    string xmlString = System.IO.File.ReadAllText(fileName);
                    StringReader sr = new StringReader(xmlString);
                    data = (XMLSettings)serializer.Deserialize(sr);
                }
                catch
                {
                    //System.Windows.Forms.MessageBox.Show("读取 XMLConfigGlobal.xml 配置文件失败，将使用默认值。", "警告", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                    data = new XMLSettings();
                    try
                    {
                        SaveSettings(data);
                    }
                    catch { }
                }
            }

            return data;
        }

        public static void SaveSettings(XMLSettings data)
        {
            string fileName = "XMLConfig.xml";
            XmlSerializer serializer;

            try
            {
                serializer = new XmlSerializer(typeof(XMLSettings));
            }
            catch (Exception e)
            {
                throw new Exception("序列化失败,错误信息是:" + e.Message);
            }

            // serialize the object
            FileStream fs = new FileStream(fileName, FileMode.Create);
            serializer.Serialize(fs, data);
            fs.Close();
        }

    #endregion
    }
}
